package com.demo.controller;


import cn.hutool.core.date.DateUtil;
import com.aliyun.oss.OSS;
import com.demo.model.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class FileController {

    @Autowired
    private OSS ossClient; //爆红没关系 会自动注入oss

    @Value("${oss.bucket.name:coin-exchange-imgs7}")
    private String bucketName;

    @Value("${spring.cloud.alicloud.oss.endpoint:oss-cn-beijing.aliyuncs.com}")
    private String endPoint;

    @PostMapping("/image/AliYunImgUpload")
    public R<String> fileUpload(@RequestParam("file")MultipartFile file) throws IOException {
        //上传文件的参数
//        1.bucketName
//        2.文件的名称
//        3.文件的输入流
        String fileName = DateUtil.today().replaceAll("-","/")+"/"+file.getOriginalFilename();
        ossClient.putObject(bucketName, fileName, file.getInputStream());
        //https://exchange-imgs.oss-cn-beijing.aliyuncs.com/2020/10/10/1588586643_meitu_1.jpg
        return R.ok("//https://"+bucketName+"."+endPoint+"/"+fileName); //http:
    }
}
